package com.cash.loan.modular.cash.service.impl;

import com.cash.loan.core.upload.QiniuService;
import com.cash.loan.modular.cash.dao.ProductAdPictureMapper;
import com.cash.loan.modular.cash.model.ProductAdPicture;
import com.cash.loan.modular.cash.result.PageResult;
import com.cash.loan.modular.cash.service.AdService;
import com.cash.loan.modular.cash.util.PageBeanUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @Date 2018-07-12 15:59
 */
@Service
public class AdServiceImpl implements AdService {

    @Autowired
    private ProductAdPictureMapper adPictureMapper;

    @Autowired
    private QiniuService qiniuService;

    @Override
    public PageResult<Map<String,Object>> adList(Integer currentPage, Integer pageSize,
                                                 String keyword,Integer cateId) {

        PageHelper.startPage(currentPage,pageSize);

        List<Map<String,Object>> result = adPictureMapper.getAdList(keyword,cateId);

        List<Map<String,Object>> afterDealResult = new ArrayList<>();

        result.stream().forEach(map -> {
            String adImgUrl = qiniuService.getUrl((String) map.get("adImgUrl"));
            map.put("adImgUrl",adImgUrl);
            if(StringUtils.isBlank((String)map.get("cateName"))) {
                map.put("cateName","");
            }
            afterDealResult.add(map);
        });

        PageResult<Map<String,Object>> pageResult = PageBeanUtil.toPageResult(result);

        pageResult.setDataList(afterDealResult);

        return pageResult;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public Boolean updateUseStatus(Long id, Long isUsed) {
        ProductAdPicture adPicture = new ProductAdPicture();
        adPicture.setId(id);
        adPicture.setIsUsed(isUsed);
        adPictureMapper.updateById(adPicture);
        return true;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public Boolean saveProductAd(Long productId, String adImgUrl, String adProductH5Url,
                                 Long isUsed,Integer cateId) {
        ProductAdPicture adPicture = new ProductAdPicture();
        adPicture.setProductId(productId);
        adPicture.setAdImgUrl(adImgUrl);
        adPicture.setAdProductH5Url(adProductH5Url);
        adPicture.setIsUsed(isUsed);
        adPicture.setCateId(cateId);
        adPictureMapper.insert(adPicture);
        return true;
    }

    @Override
    public Boolean updateAd(Long productId, String adImgUrl,String adProductH5Url,
                            Long isUsed, Long id,Integer cateId) {
        ProductAdPicture adPicture = new ProductAdPicture();
        adPicture.setProductId(productId);
        adPicture.setAdImgUrl(adImgUrl);
        adPicture.setId(id);
        adPicture.setAdProductH5Url(adProductH5Url);
        adPicture.setIsUsed(isUsed);
        adPicture.setCateId(cateId);
        adPictureMapper.updateById(adPicture);
        return true;
    }

    @Override
    public Map<String, Object> getOneById(Long id) {
        Map<String,Object> map = adPictureMapper.getOneById(id);
        String adImgUrl = qiniuService.getUrl((String) map.get("adImgUrl"));
        map.put("adImgUrl",adImgUrl);
        if(map.get("adProductH5Url") == null) {
            map.put("adProductH5Url","");
        }
        if(map.get("cateId") == null) {
            map.put("cateId",0);
        }
        return map;
    }
}
